/**
 * 
 */
package com.evon.yardmanagement.service;

import java.util.List;

import com.evon.yardmanagement.model.KeyBin;
import com.evon.yardmanagement.model.KeyBox;
import com.evon.yardmanagement.model.ParkingSlot;
import com.evon.yardmanagement.model.Section;
import com.evon.yardmanagement.dao.KeyBinDAO;
import com.evon.yardmanagement.dao.KeyBoxDAO;
import com.evon.yardmanagement.dao.SectionDAO;
import com.evon.yardmanagement.model.Vendor;
import com.evon.yardmanagement.model.KeyBox;
import com.evon.yardmanagement.transformer.BaseTransformer;
import com.evon.yardmanagement.ui.dto.KeyBinDTO;
import com.evon.yardmanagement.ui.dto.KeyBoxDTO;
import com.evon.yardmanagement.ui.dto.ParkingSlotDTO;

/**
 * @author Sandeep
 *
 */
public class KeyBoxManagementService implements IKeyBoxManagementService {

	/* (non-Javadoc)
	 * @see com.evon.yardmanagement.service.IKeyBoxManagementService#addKeyBox(com.evon.yardmanagement.model.IKeyBox)
	 */
	
	  KeyBoxDAO keyBoxDao = new KeyBoxDAO(); 
	  

		BaseTransformer<KeyBox, KeyBoxDTO> transformer = new BaseTransformer<KeyBox, KeyBoxDTO>(
				KeyBox.class, KeyBoxDTO.class);

	/* (non-Javadoc)
	 * @see com.evon.yardmanagement.service.IKeyBoxManagementService#deactivateKeyBin(com.evon.yardmanagement.model.IKeyBox)
	 */
	public boolean deactivateKeyBin(KeyBox keybox) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.evon.yardmanagement.service.IKeyBoxManagementService#getAllKeyBoxes()
	 */
	public List<KeyBox> getAllKeyBoxes() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public List<KeyBox> getAvailableKeyForForYard(int yardId){
		return keyBoxDao.getAvailableKeyBoxForYard(yardId);
	}

	/* (non-Javadoc)
	 * @see com.evon.yardmanagement.service.IKeyBoxManagementService#getkeyBinDetails(com.evon.yardmanagement.model.ICompany)
	 */
	public List<KeyBox> getkeyBinDetails(Vendor company) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public boolean addKeyBox(KeyBox keyBox) {
		try {keyBoxDao.saveOrUpdate(keyBox);
		return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	

	public List<KeyBox> fetchKeyBoxByKeyBinId(Integer id){
		KeyBin keyBin = new KeyBin();
		keyBin.setId(id);
		return keyBoxDao.findByProperty("keyBin", keyBin);
	}
	
	public boolean addKeyBoxes(List<KeyBoxDTO> kbDTO, int sectionId) {
		if (kbDTO == null || kbDTO.isEmpty()) {
			return false;
		}
		List<KeyBox> kbList = transformer.transformToModel(kbDTO);
		KeyBin section = (KeyBin) new KeyBinDAO().findById(sectionId);
		boolean isSuccess = true;
		for (KeyBox kb : kbList) {
			kb.setKeyBin(section);
			isSuccess = isSuccess && addKeyBox(kb);
		}

		return isSuccess;
	}
}
